import numpy as np


input_example = [[-0.783778, 1.783778, 0.026, 0.0, 0.000431, 0.002889, 0.000444, 0.059111, 0.100889, 2145302.755556, 0.135066, 0.004496, 0.073298, 0.864934, 0.003251, 0.004196, 1225196032.0, 498422272.0, 35434.666667, 36029.866667, 23257771.25, 2145302.755556],
 [-0.863111, 1.863111, 0.072889, 0.0, 0.0, 0.000889, 0.0, 0.026222, 0.030667, 563609.6, 0.079898, 0.003494, 0.048774, 0.920102, 0.005568, 0.00384, 616874496.0, 967455232.0, 2587.2, 3118.066667, 3363980.0, 563609.6],
 [-0.773778, 1.773778, 0.155333, 0.0, 0.0, 0.000444, 0.0, 0.024889, 0.036, 6830671.644444, 0.081268, 0.00348, 0.049573, 0.918732, 0.005644, 0.003863, 634319360.0, 965734912.0, 2614.533333, 3022.066667, 3338474.5, 6830671.644444],
 [-0.777333, 1.777333, 0.018889, 0.0, 0.000222, 0.003556, 0.0, 0.054, 0.110667, 8691165.866667, 0.15591, 0.006623, 0.088744, 0.84409, 0.003981, 0.004622, 1398518272.0, 1943880192.0, 31833.466667, 32364.333333, 23702986.0, 8691165.866667]]

output_example = [[-7.83778e-01,  1.783778e+00,  2.6e-02, 0, -1,  2.889e-03, -1, -1,1.00889e-01, 2145302.755556,  1.35066e-01,  4.496e-03, 7.3298e-02,  8.64934e-01, -1,  4.196e-03,
1225196032.0, -1, -1, -1, 23257771.25, 2145302.755556],
 [-1, -1,  7.2889e-02, 0, 0,  8.89e-04, 0,  2.6222e-02, -1, -1, -1,  3.494e-03,
  -1, -1,  5.568e-03, -1, -1,  9.67455232e+08, -1,  3118.066667,3.36398e+06, -1],
 [-1, -1, -1, 0, 0, -1, 0, -1, 3.6e-02,  6830671.644444,  8.1268e-02, -1,
   4.9573e-02,  9.18732e-01, -1,  3.863e-03, 6.34319360e+08,  9.65734912e+08,  2614.533333, -1, -1,  6830671.644444],
 [-7.77333e-01,  1.777333, -1, 0, 2.22e-04, -1, 0,  5.4e-02, -1, -1, -1, -1,
  -1, -1,  0.003981, -1, -1, -1,  31833.466667, 32364.333333, -1, -1]]


def compare(o_e, o):
    assert isinstance(o_e, np.ndarray)
    assert isinstance(o, np.ndarray)
    if o_e.shape == o.shape:

        temp = o_e == o
        flag = True
        for row in temp:
            for col in row:
                flag = flag & col
        return flag
    else:
        return False


def ourpreprocess(temp_arr):
    no=temp_arr.shape[1]
    DSIZE=temp_arr.shape[0]
    for num in range(0,no):
        value=temp_arr[:,num]
        npArray=dataPreprocess(value,DSIZE)
        temp_arr[:, num]=npArray
    return temp_arr


def percent_range(dataset,DSIZE, min=0.20, max=0.80):
    range_max = np.percentile(dataset, max * 100)
    range_min = -np.percentile(-dataset, (1 - min) * 100)
    result=np.empty((DSIZE,))
    i=0
    for value in dataset:
        if value <= range_max and value >= range_min:
            result[i]=dataset[i]
        else:
            result[i]=-1

        i+=1
    return result


def dataPreprocess(npArray,DSIZE):
    npArray = percent_range(npArray, DSIZE, 0.025, 0.975)
    return npArray


# 用例目的
print("该用例目的为：")
print("进行离群点处理组件功能测试，测试该组件在正常数据输入时能否获得预定输出，该处理后，离群点将被删除，用-1补位")

# 子用例编号
print("子用例编号：")
print("Outlier_2")

print("****************************")
print("当前输入为：")
# 输出用例设置
print(input_example)
# 输出用例设置

print("")

print("****************************")
print("当前输出为:")
# 输出处理后数据

output = ourpreprocess(np.array(input_example))
print(output)
# 输出处理后数据

print("****************************")
print("是否正确:")
# 输出对比结果
# 需要写一个compare函数
data = compare(np.array(output_example), output)
if data:
    # print(f"原数组方差为：${data[0]}")
    # print(f"当前数组方差为：${data[1]}，小于原始数组")
    print("输出与预定目标相符")
else:
    print("输出与预定目标不符")
# 输出对比结果

print("\n")
